package com.lwb.eduorder.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lwb.commonutils.JwtUtils;
import com.lwb.commonutils.Row;
import com.lwb.eduorder.entity.TOrder;
import com.lwb.eduorder.service.TOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-12-10
 */
@RestController
@RequestMapping("/eduorder/t-order")
@CrossOrigin
public class TOrderController {

    @Autowired
    private TOrderService tOrderService;

    @PostMapping("createOrder/{courseId}")
    public Row createOrder(@PathVariable String courseId, HttpServletRequest request){
        String jwtToken = JwtUtils.getMemberIdByJwtToken(request);
        //创建订单，返回订单
        String ordeNo = tOrderService.createOrder(courseId,jwtToken);
        return Row.SUCCESS().data("orderId",ordeNo);
    }

    @PostMapping("getOrderById/{orderNo}")
    public Row getOrderById(@PathVariable String orderNo){
        TOrder tOrder = tOrderService.getOrderById(orderNo);
        return Row.SUCCESS().data("tOrder",tOrder);
    }

    @GetMapping("isBuyCourse/{courseId}/{memberId}")
    public Boolean isBuyCourse(@PathVariable String courseId,@PathVariable String memberId){
        QueryWrapper<TOrder> tOrderQueryWrapper = new QueryWrapper<>() ;
        tOrderQueryWrapper.eq("course_id",courseId);
        tOrderQueryWrapper.eq("member_id",memberId);
        tOrderQueryWrapper.eq("status",1);

        int count = tOrderService.count(tOrderQueryWrapper);
        if(count>0){
            return true;
        }else{
            return false;
        }
    }

}

